package com.kuang.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kuang.common.Result;
import com.kuang.pojo.Notice;
import com.kuang.pojo.User;
import com.kuang.service.NoticeService;
import com.kuang.service.UserService;
import com.kuang.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    NoticeService noticeService;

    @Autowired
    UserService userService;

    //查询功能
    @GetMapping("/select")
    public Result selectAllUser(){
        //添加条件构造器
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        //设置排序条件
        wrapper.orderByDesc("id");
        //List<User> users = userMapper.selectAllUser();
        List<Notice> notices = noticeService.list(wrapper);
        for (Notice notice : notices) {
            System.out.println(notice);
        }
        return Result.success(notices);
    }


    /**
     * 通过id删除新闻
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){

        noticeService.removeById(id);
        return Result.success();
    }

    /**
     * 通过id批量删除新闻
     * @param ids
     * @return
     */
    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids){
        noticeService.removeBatchByIds(ids);
        return Result.success();
    }
    //49
    //修改用户
    @PutMapping("/update")
    public Result update(@RequestBody Notice notice){
        noticeService.updateById(notice);
        return Result.success();
    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @param title
     * @return
     */
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam("pageNum") Integer pageNum,
                               @RequestParam("pageSize") Integer pageSize,
                               @RequestParam("title") String title){
        //构造分页查询器
        Page<Notice> pageInfo = new Page<>(pageNum,pageSize);
        //构造条件查询器
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        //添加一个排序条件
        wrapper.orderByAsc("id");
        //添加模糊查询
        wrapper.like(StringUtils.isNotBlank(title),"title",title);
        //执行查询
        Page<Notice> page = noticeService.page(pageInfo, wrapper);
        //获取每次查询的结果
        List<Notice> records = page.getRecords();
        //从每次查询的结果中获取作者id
        for (Notice record : records) {
            Integer userId = record.getUserid();
            //根据作者id查询出来用户信息
            User user = userService.getById(userId);
            //将用户名字添加到record中
            if (user != null)
                record.setUser(user.getName());
        }

        //返回数据
        return Result.success(pageInfo);
    }

    /**
     * 新增用户
     * @param notice
     * @return
     */
    @PostMapping("/add")
    public Result addNews(@RequestBody Notice notice){
        //获取当前用户信息
        User currentUser = TokenUtils.getCurrentUser();
        //将用户id设置为作者id
        notice.setUserid(currentUser.getId());
        //设置时间
        notice.setTime(DateUtil.now());
        noticeService.save(notice);
        return Result.success();
    }

    /**
     * 查询公告
     * @return
     */
    @GetMapping("/selectUserData")
    public Result selectUserData(){
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        //只能查询公开的公告
        wrapper.eq("open",1);
        wrapper.orderByDesc("time");
        List<Notice> list = noticeService.list(wrapper);
        return Result.success(list);
    }

}
